﻿
var articleId = ""; //文章id
var advertId = ""; //广告id
var url = ""; //外部链接
var logo = ""; //文章logo
var type = 1; //分享类型
var title = "";
$(function () {
    initVideo();
    img();
    phone();
    articleId = getQueryString("article_id");
    advertId = getQueryString("advert_id");
    url = url = $("#url").val();
    type = $("#type").val();
    type = type.length == 0 ? 1 : type;
    logo = $("#logo").val();
    title = document.title;
    if (title == "")
        title = getQueryString("url_title");

    if (type == 2) {
        if (url.indexOf(window.location.host) != -1) {
            var istrue = false;
            for (var i = 0; i < filter_url.length; i++) {
                if (url.indexOf(filter_url[i]) > -1) {
                    istrue = true;  //排除指定页面
                    break;
                }
            }
            if (!istrue) {
                toast.show("不允许分享内部网页");
                return;
            }
        }
        if (url.toLowerCase().indexOf("//mp.weixin.qq.com") > -1) {
            // 微信订阅号文章、今日头条 单独处理
            loadWxUrl(url);
        } else {
            $(".ifm-body").append("<iframe id = 'ifmUrl' onLoad='ifmLoad();' src = '" + url + "'></iframe>");
        }
        $("#con_body").remove();
    }
    else {

        loadArticle();  //加载文章内容
    }
    loadAd();           //加载广告

    //关闭广告
    $("#ad_close").bind("click", function () { $(".replace").remove(); $(".advert").remove(); });

    //更改广告
    $("#ad_choose").bind("click", function () {
        window.location.href = 'Selectshare.html?' + document.location.search.replace(/\?/gi, '');
    });

    var is_app = "";

    //设置编辑模式
    if (is_app.length > 0) {
        $("body").removeClass();
        $("body").addClass("dis-bar");
    }
    else {
        $("#ad_choose").remove();
    }

    //分享图标点击事件
    $("#share_ico > .row > div").bind("click", sharedClick);
    $(".shared-part [data]").bind("click", sharedClick);

    //广告点击事件
    $("#advert_img").bind("click", function () {
        var param = getAdParam();
        window.open("to_advert.aspx?" + param);
    });

    //收藏按钮
    $(".top-right").bind("click", function () {
        collect();
    });
});

//电话为空时隐藏
function phone() {
    if ($("#phone").val() == "") {
        $(".playphone").css("display", "none");
    }
}

function img() {
    $(".row img").each(function () {
        if (imgsub($(this).parent().parent().parent().parent().css("width")) > $(window).width()) {
            $(this).parent().parent().parent().parent().css("width", "100%")
        }
        if (imgsub($(this).parent().parent().parent().css("width")) > $(window).width()) {
            $(this).parent().parent().parent().css("width", "100%");
        }
        if (imgsub($(this).parent().parent().css("width")) > $(window).width())
        { $(this).parent().parent().css("width", "100%"); }
        if (imgsub($(this).parent().css("width")) > $(window).width())
        { $(this).parent().css("width", "100%"); }
        if (imgsub($(this).css("width")) > $(window).width())
        { $(this).css("width", "100%"); }

    });

    $(".col-xs-12 div").each(function () {
        if (imgsub($(this).css("width")) > 640)
        { $(this).css("width", "100%"); }
    });
}
function imgsub(width) {
    return width.substr(0, width.length - 2);
}





//收藏该文章
function collect() {
    //登录验证
    if (!islogin()) {
        window.location.href = "login.html"
    }

    var param = "article_id=" + articleId + "&url=" + encodeURIComponent(url) + "&type=" + type
    ajax.Addcollection(param, function (data) {
        if (data.code != 0) {
            toast.show(data.msg);
            return;
        }
        toast.show("收藏成功");
    });
}

//分享按钮点击事件
function sharedClick() {
    var fx = $(this).attr("data");
    share(fx);  //没有登录直接分享
}

var number = Math.random().toString().substr(2, 1);
//加载文章内容
function loadArticle() {
    //如果广告id存在
    if (articleId.length == 0 || articleId == 0) {
        return;
    }
    ajax.ArticleClick("id=" + articleId + "&number=" + number, function (data) {
        if (data.code != 0) {
            toast.show(data.msg);
            return;
        }
    });
}

//加载广告
function loadAd() {
    //如果广告id存在
    if (advertId.length == 0 || advertId == 0) {
        $("#detail_con").removeClass();
        $("#detail_con").addClass("advert-bottom");
        return;
    }

    var param = getAdParam();
    param += "&number=" + number;
    ajax.DetailArticle(param, function (data) {
        $("#detail_con").removeClass();
        if (data.code == 1 && (!islogin())) {
            $("#detail_con").addClass("advert-bottom");
            return; //找不到广告ID与文章对应的文章
        }

        if (data.code != 0) {
            $("#detail_con").addClass("advert-bottom");
            toast.show(data.msg);
            return;
        }

        //更换广告图标
        $(".replace img").attr("src", data.item.img);
        $("#advert_img").attr("src", data.item.img);

        $("#detail_con").removeClass();
        switch (data.item.type) {
            case 1: $("#detail_con").addClass("advert-top"); break;
            case 2: $("#detail_con").addClass("advert-bottom"); break;
            case 3: $("#detail_con").addClass("advert-float"); break;
            default: $("#detail_con").addClass("advert-bottom"); break;
        }
    });
}

//获取广告参数
function getAdParam() {
    var param = "type=" + type + "&id=" + advertId;
    if (getQueryString("shareid") != '')
        param += "&shareid=" + getQueryString("shareid");
    //获取广告是上传分享的信息  便于统计点击量
    if (type == 2)
        param += "&url=" + encodeURIComponent(url);
    else
        param += "&article_id=" + articleId;
    return param;
}

//获取?传值
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return "";
}

//分享
function share(a) {
    //如果是分享到微信
    if (a == "weixin") {
        var ua = navigator.userAgent;
        //判断是否在微信里打开的
        if (ua.indexOf("MicroMessenger") != -1) {
            toast.show("请点击右上角按钮进行分享");
        }
        else {
            toast.show("请用微信打开进行分享");
        }
        return;
    }

    var host = shared_domain;   //分享的域名,在base.js中配置
    var title = "";
    var summary = "点击查看详情";

    var share_url = host + "/client/detail.aspx?advert_id=" + advertId;
    if (type == 2) {
        //        if (!CheckUrl(url)) {
        //            toast.show(url + "  不是一个有效的网址");
        //            return;
        //        }
        title = "网页分享";
        share_url += "&type=2&url=" + url;
    }
    else {
        title = $.trim($("#title").text());
        share_url += "&type=1&article_id=" + articleId;
    }

    jiathis_sendto(a, share_url, title, summary, logo);
}

function copyUrl() {
    var url = window.location.href;
    if (url.indexOf("is_app=1&") > -1) {
        url = url.replace("is_app=1&", "");
    }
    if (url.indexOf("is_app=1") > -1) {
        url = url.replace("is_app=1", "");
    }
    prompt('您的浏览器不支持复制功能，请手动复制！', url);

}


//微信订阅号文章 单独处理
function loadWxUrl(url) {
    $.post("/Client/Ajax/HttpSource.ashx?GetSource", { url: url }, function (data) {
        window.ifm = data.item;
        $(".ifm-body").append("<iframe id = 'ifmUrl' src = 'javascript:parent.ifm'></iframe>");
        ifmLoad();
    }, "json");

}



//广告跳转
function toAd(id) {
    //var toUrl = "to_advert.aspx?shareid=" + getQueryString("shareid") + "&id=" + id + "&type=" + type + "&url=" + encodeURI(url);
    var toUrl = "to_advert.aspx?shareid=" + getQueryString("shareid") + "&id=" + id + "&type=" + type;
    //判断是否苹果端的app
    var ua = navigator.userAgent;
    if (ua.indexOf("wjx") != -1 && ua.indexOf("iPhone") != -1) {
        window.location.href = "callback://ad?" + encodeURI("http://wjx.711688.com/client/" + toUrl);
    }
    else {
        window.open(toUrl);
    }
}


function ifmLoad() {
    setTimeout(function () {

        //解决ios微信链接内容显示不全Bug
        if ($("#ifmUrl").attr("src").indexOf("http") != 0 && navigator.userAgent.toLowerCase().indexOf("iphone os") != -1) {
            $("#ifmUrl").contents().find("body").css("width", $(window).width() + "px");
        }

        $("iframe").contents().find("[data-src]").each(function () {
            $(this).attr("src", $(this).attr("data-src"));
        });
        reloadWxVideo();
    }, 500);
}


//处理微信视频
function reloadWxVideo() {
    var iframeDoc = $("#ifmUrl").contents();

    var videoFrame = $(iframeDoc).find("iframe");

    //判断是否视频
    if (videoFrame.length > 0) {

        $(videoFrame).each(function () {


            var videoUrl = $(this).attr("src");
            $($(this)).removeAttr("src");

            //提取视频id
            var vid = videoUrl.substring(videoUrl.indexOf("vid="))
            vid = vid.substring(4, vid.indexOf("&"));

            //视频页面 不含参数
            var videoHost = videoUrl.substring(0, videoUrl.indexOf("?")).replace("preview", "player");

            //修改后的视频宽度
            var vw = $(window).width() - 30;

            //修改后的视频高度
            var vh = vw / 500 * 375

            //设置兼容微信视频
            videoUrl = videoHost + "?vid=" + vid + "&width=" + vw + "&height=" + vh + "&auto=0"
            $(this).attr("src", videoUrl);

            //解决超出的空白
            $(this).css("width", vw + "px");

            //解决超高空白
            $(this).css("height", vh + "px");

            $(this).attr("width", vw);
            $(this).attr("height", vh);
        });
    }
}

//初始化腾讯视频
function initVideo() {

    //腾讯视频
    $.each($("video[vid]"), function () {

        //得到vid
        var vid = $(this).attr("vid");
        var tag = this;

        //获取播放地址
        ajaxJson({
            url: "/Client/Ajax/HttpSource.ashx?GetTencentVideo&vid=" + vid,
            callback: function (data) {
                $(tag).attr("src", data);
            }
        });
    });

    //今日头条
    $.each($("video[videoid]"), function () {

        //得到videoid
        var videoid = $(this).attr("videoid");

        var tag = this;

        //获取播放地址
        ajaxJson({
            url: "/Client/Ajax/HttpSource.ashx?GetTTVideo&videoid=" + videoid,
            callback: function (data) {
                $(tag).attr("src", data);
            }
        });
    });
}

//头条文章 单独处理
function loadTouTiao(url) {
    $.post("/Client/Ajax/HttpSource.ashx?GetSource", { url: url }, function (data) {
        $(".ifm-body").append("<iframe id = 'ifmUrl' src = '" + url + "'></iframe>");
        ifmLoad();
    }, "json");
}